#!/usr/bin/env zsh
0=${(%):-%x}
setopt extended_glob

local T_PRJDIR="${0:A:h:h}"
cd "$T_PRJDIR"

local o_unit o_rev
zparseopts -D -M -- -unit=o_unit -rev=o_rev || return 1

testfiles=()
if (( $# > 0 )); then
  testfiles=($@)
elif (( $#o_unit )); then
  testfiles=($T_PRJDIR/tests/README.md $T_PRJDIR/tests/test_*.md~*test_real*~*foo*)
else
  testfiles=($T_PRJDIR/tests/README.md $T_PRJDIR/tests/test_*.md)
fi

# if tests are run in reverse order, I can catch places where I didn't teardown properly
if (( $#o_rev )); then
  testfiles=(${(O)testfiles})
fi

# foo example test command
# env -i PATH=$PATH FPATH=$FPATH \
#  zsh -f -- =clitest --list-run --progress dot --prompt '%' --color always $T_PRJDIR/tests/foo.md

# Use ZSH_BINARY if set, otherwise fallback to default zsh
ZSH=${ZSH_BINARY:-zsh}

env -i PATH=$PATH FPATH=$FPATH PAGER=cat \
    $ZSH -f -- \
    =clitest \
        --list-run --progress dot --prompt '%' \
        --color always \
        --pre-flight 'git --version; print $T_PRJDIR $VENDOR $OSTYPE =$ZSH $ZSH_VERSION $ZSH_PATCHLEVEL' \
        -- $testfiles
